/*
 *  Copyright (C) 2011 glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.samples;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import javax.persistence.*;
import org.balisunrise.annotation.ColumnDefinition;
import org.balisunrise.annotation.ColumnEmbedded;
import org.balisunrise.annotation.CriteriaDefinition;
import org.balisunrise.annotation.CriteriaEmbedded;
import org.balisunrise.annotation.Editor;
import org.balisunrise.annotation.FieldDefinition;
import org.balisunrise.annotation.FieldSelection;
import org.balisunrise.annotation.Filter;
import org.balisunrise.annotation.FilterCriteria;
import org.balisunrise.annotation.FilterType;
import org.balisunrise.annotation.Grid;
import org.balisunrise.annotation.GridColumn;
import org.balisunrise.annotation.Panel;
import org.balisunrise.annotation.PanelField;
import org.balisunrise.annotation.Search;
import org.balisunrise.annotation.Transaction;
import org.balisunrise.annotation.VisualEntity;
import org.hibernate.annotations.Type;

/**
 *
 * @author glauco
 */
@Entity
// <editor-fold defaultstate="collapsed" desc="VisualEntity">
@VisualEntity(transactions = {
    @Transaction(alias = "def", editor = "def", search = "def", title = "Cadastro de CEP")},
searchs = {
    @Search(alias = "def", filter = "def", grid = "def", title = "Pesquisa de CEP")},
editors = {
    @Editor(alias = "def", panel = "def", title = "CEP")},
panels = {
    @Panel(alias = "def", title = "CEP")},
filters = {
    @Filter(alias = "def", title = "Filtros de Pesquisa de CEP")},
grids = {
    @Grid(alias = "def", title = "CEP's")})
// </editor-fold>
public class CEP implements Serializable {

    // <editor-fold defaultstate="collapsed" desc="PropertyChangeSupport">
    @Transient
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);

    public void addPropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(listener);
    }

    public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
    }
    public static final String PROP_CEP = "cep";
    public static final String PROP_CIDADE = "cidade";
    public static final String PROP_BAIRRO = "bairro";
    public static final String PROP_LOGRADOURO = "logradouro";
    public static final String PROP_NUMERO = "numero";
    // </editor-fold>

    public CEP() { }

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Id
    @Column(length = 9, columnDefinition = "char(9)", nullable = false)
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(
        definitions=@FieldDefinition(alias="def",label="CEP"))
    @GridColumn(
        definitions=@ColumnDefinition(alias="def",name="CEP",width=80))
    // </editor-fold>
    private String cep;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @ManyToOne
    @PanelField(selection = {
        @FieldSelection(alias = "def", panel = "sel", search = "def",
        notFoundMessage = "Cidade não encontrada!")})
    @FilterCriteria(embedded=
        @CriteriaEmbedded(alias="def",filter="def",index=1))
    @GridColumn(embedded=
        @ColumnEmbedded(alias="def",grid="emb",index=1))
    // </editor-fold>
    private Cidade cidade;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 50, columnDefinition = "char(50)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "Bairro",index=10)})
    @FilterCriteria(definitions=
        @CriteriaDefinition(alias="def",filterType=FilterType.ILIKE,label="Bairro",index=10))
    @GridColumn(
        definitions=@ColumnDefinition(alias="def",name="Bairro",index=10))
    // </editor-fold>
    private String bairro;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 50, columnDefinition = "char(50)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "Logradouro",index=20)})
    @FilterCriteria(definitions=
        @CriteriaDefinition(alias="def",filterType=FilterType.ILIKE,label="Logradouro",index=20))
    @GridColumn(
        definitions=@ColumnDefinition(alias="def",name="Logradouro",index=20))
    // </editor-fold>
    private String logradouro;

    // <editor-fold defaultstate="collapsed" desc="annotations">
    @Column(length = 10, columnDefinition = "char(10)")
    @Type(type = "org.balisunrise.dao.hibernate.TrimmedString")
    @PanelField(definitions = {
        @FieldDefinition(alias = "def", label = "Nº")})
    // </editor-fold>
    private String numero;

    private java.util.List<Endereco> enderecos;

    @Transient
    private Integer inter;

    // <editor-fold defaultstate="collapsed" desc="Get's & Set's">
    public String getNumero() {
        return numero;
    }

    public void setNumero(String numero) {
        String oldNumero = this.numero;
        this.numero = numero;
        propertyChangeSupport.firePropertyChange(PROP_NUMERO, oldNumero, numero);
    }

    public String getBairro() {
        return bairro;
    }

    public void setBairro(String bairro) {
        String oldBairro = this.bairro;
        this.bairro = bairro;
        propertyChangeSupport.firePropertyChange(PROP_BAIRRO, oldBairro, bairro);
    }

    public String getLogradouro() {
        return logradouro;
    }

    public void setLogradouro(String logradouro) {
        String oldLogradouro = this.logradouro;
        this.logradouro = logradouro;
        propertyChangeSupport.firePropertyChange(PROP_LOGRADOURO, oldLogradouro, logradouro);
    }

    public Cidade getCidade() {
        return cidade;
    }

    public void setCidade(Cidade cidade) {
        Cidade oldCidade = this.cidade;
        this.cidade = cidade;
        propertyChangeSupport.firePropertyChange(PROP_CIDADE, oldCidade, cidade);
    }

    public String getCep() {
        return cep;
    }

    public void setCep(String cep) {
        String oldCep = this.cep;
        this.cep = cep;
        propertyChangeSupport.firePropertyChange(PROP_CEP, oldCep, cep);
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="Equals, Hash, To String">
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final CEP other = (CEP) obj;
        if ((this.cep == null) ? (other.cep != null) : !this.cep.equals(other.cep)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.cep != null ? this.cep.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "CEP{" + "cep=" + cep + "cidade=" + cidade + "bairro=" + bairro
                + "logradouro=" + logradouro + "numero=" + numero + '}';
    }
    // </editor-fold>
}
